﻿Imports TBCC.UtilityMobile
Public Class ProjectType

    'Private _projectConfigFile As String

    Public Sub New(ByVal pi As ProjectItem)
        '_projectConfigFile = Application.StartupPath & String.Format("/Projects/{0}/projectConfig.dat", pi.ProjectID.Trim())
        _pi = pi
    End Sub

    Private _pi As ProjectItem

#Region ""

    Private Class TableNames
        Public Const const_table_CompressorGroup As String = "CompressorGroup"
        Public Const const_table_Compressor As String = "Compressor"
    End Class

    Private Class TableCompressorGroup
        Public Const GroupID As String = "GroupID"
        Public Const GroupName As String = "GroupName"
        Public Const DateCreated As String = "DateCreated"
        Public Const ListNo As String = "ListNo"
        Public Const NetAddress As String = "NetAddress"
        Public Const ProjectID As String = "ProjectID"
    End Class

    Private Class TableCompressor
        Public Const GroupID As String = "GroupID"
        Public Const CompressorID As String = "CompressorID"
        Public Const CompressorName As String = "CompressorName"
        Public Const IsUsed As String = "isUsed"
    End Class


#End Region

    'Private Function CreateDataset() As DataSet
    '    Dim ds As New DataSet("ProjectConfig")

    '    ds.Tables.Add(Me.CreateDatatable_CompressorGroup)
    '    ds.Tables.Add(Me.CreateDatatable_Compressor)
    '    ds.Tables.Add(Me.CreateDatatable_Condenser)
    '    ds.Tables.Add(Me.CreateDatatable_Fan)

    '    Return ds
    'End Function

    'Private Function GetDataset() As DataSet
    '    Dim ds As DataSet = Nothing

    '    If System.IO.File.Exists(_projectConfigFile) Then
    '        ds = New DataSet
    '        ds.ReadXml(_projectConfigFile)
    '    Else
    '        ds = CreateDataset()
    '    End If

    '    Return ds
    'End Function

    ''' <summary>
    ''' 返回压缩机组列表
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetCompressorGroupList() As IList(Of ProjectTypeItem)
        Dim il As IList(Of ProjectTypeItem)
        il = New List(Of ProjectTypeItem)

        Dim ds As DataSet

        Dim strSql As String
        strSql = String.Format("Select * From tbccCompressorGroup where projectID='{0}' ;Select * From tbccCompressor;Select * From tbccFans  ", _pi.ProjectID)

        ds = TBCC.UtilityMobile.SqlHelper.ExecuteDataset(strSql, Nothing)

        For Each r As DataRow In ds.Tables(0).Rows
            Dim cgi As New ProjectTypeItem()
            cgi.GroupID = r("GroupID")
            cgi.GroupName = r("GroupName")
            cgi.NetAddress = r(TableCompressorGroup.NetAddress)
            cgi.projectID = r(TableCompressorGroup.ProjectID)

            Dim filter As String
            filter = String.Format("GroupID='{0}'", r("GroupID"))
            For Each r2 As DataRow In ds.Tables(1).Select(filter)
                Select Case CInt(r2("compressorID"))
                    Case 1
                        cgi.hasCompressor1 = IIf(r2("IsUsed") = 1, True, False)

                    Case 2
                        cgi.hasCompressor2 = IIf(r2("IsUsed") = 1, True, False)

                    Case 3
                        cgi.hasCompressor3 = IIf(r2("IsUsed") = 1, True, False)

                    Case 4
                        cgi.hasCompressor4 = IIf(r2("IsUsed") = 1, True, False)

                    Case 5
                        cgi.hasCompressor5 = IIf(r2("IsUsed") = 1, True, False)

                    Case 6
                        cgi.hasCompressor6 = IIf(r2("IsUsed") = 1, True, False)

                    Case Else

                End Select
            Next

            For Each r2 As DataRow In ds.Tables(2).Select(filter)
                Select Case CInt(r2("fanID"))
                    Case 1
                        cgi.hasFan1 = IIf(r2("IsUsed") = 1, True, False)

                    Case 2
                        cgi.hasFan2 = IIf(r2("IsUsed") = 1, True, False)

                    Case 3
                        cgi.hasFan3 = IIf(r2("IsUsed") = 1, True, False)

                    Case 4
                        cgi.hasFan4 = IIf(r2("IsUsed") = 1, True, False)

                    Case 5
                        cgi.hasFan5 = IIf(r2("IsUsed") = 1, True, False)

                    Case 6
                        cgi.hasFan6 = IIf(r2("IsUsed") = 1, True, False)

                    Case Else

                End Select
            Next

            il.Add(cgi)
        Next

        Return il

    End Function

    Public Shared Function GetCompressorGroupModel(ByVal id As String) As ProjectTypeItem
        Dim ds As DataSet

        Dim strSql As String
        strSql = String.Format("Select * From tbccCompressorGroup where groupID='{0}' ;Select * From tbccCompressor where groupID='{0}';Select * From tbccFans  where groupID='{0}'", id)

        ds = TBCC.UtilityMobile.SqlHelper.ExecuteDataset(strSql, Nothing)

        Dim cgi As ProjectTypeItem = Nothing

        For Each r As DataRow In ds.Tables(0).Rows

            cgi = New ProjectTypeItem

            cgi.GroupID = r("GroupID")
            cgi.GroupName = r("GroupName")
            cgi.NetAddress = r(TableCompressorGroup.NetAddress)
            cgi.projectID = r(TableCompressorGroup.ProjectID)

            Dim filter As String
            filter = String.Format("GroupID='{0}'", r("GroupID"))
            For Each r2 As DataRow In ds.Tables(1).Select(filter)
                Select Case CInt(r2("compressorID"))
                    Case 1
                        cgi.hasCompressor1 = IIf(r2("IsUsed") = 1, True, False)

                    Case 2
                        cgi.hasCompressor2 = IIf(r2("IsUsed") = 1, True, False)

                    Case 3
                        cgi.hasCompressor3 = IIf(r2("IsUsed") = 1, True, False)

                    Case 4
                        cgi.hasCompressor4 = IIf(r2("IsUsed") = 1, True, False)

                    Case 5
                        cgi.hasCompressor5 = IIf(r2("IsUsed") = 1, True, False)

                    Case 6
                        cgi.hasCompressor6 = IIf(r2("IsUsed") = 1, True, False)

                    Case Else

                End Select
            Next

            For Each r2 As DataRow In ds.Tables(2).Select(filter)
                Select Case CInt(r2("fanID"))
                    Case 1
                        cgi.hasFan1 = IIf(r2("IsUsed") = 1, True, False)

                    Case 2
                        cgi.hasFan2 = IIf(r2("IsUsed") = 1, True, False)

                    Case 3
                        cgi.hasFan3 = IIf(r2("IsUsed") = 1, True, False)

                    Case 4
                        cgi.hasFan4 = IIf(r2("IsUsed") = 1, True, False)

                    Case 5
                        cgi.hasFan5 = IIf(r2("IsUsed") = 1, True, False)

                    Case 6
                        cgi.hasFan6 = IIf(r2("IsUsed") = 1, True, False)

                    Case Else

                End Select
            Next

            Continue For
        Next

        Return cgi
    End Function

    'Public Sub _AddCompressorGroup(ByVal item As CompressorGroupItem)
    '     Dim ds As DataSet
    '    ds = GetDataset()

    '    Dim dt As DataTable = ds.Tables(TableNames.const_table_CompressorGroup)

    '    Dim r As DataRow
    '    r = dt.NewRow
    '    r(TableCompressorGroup.GroupID) = item.GroupID
    '    r(TableCompressorGroup.GroupName) = item.GroupName
    '    r(TableCompressorGroup.NetAddress) = item.NetAddress
    '    r(TableCompressorGroup.DateCreated) = Now.ToString("yyyy-MM-dd HH:mm:ss")
    '    r(TableCompressorGroup.ListNo) = 0
    '    dt.Rows.Add(r)

    '    Dim dt2 As DataTable = ds.Tables(TableNames.const_table_Compressor)
    '    Dim r2 As DataRow

    '    r2 = dt2.NewRow
    '    r2(TableCompressor.CompressorID) = 1
    '    r2(TableCompressor.IsUsed) = item.hasCompressor1
    '    r2(TableCompressor.GroupID) = item.GroupID
    '    r2(TableCompressor.CompressorName) = " "
    '    dt2.Rows.Add(r2)

    '    r2 = dt2.NewRow
    '    r2(TableCompressor.CompressorID) = 2
    '    r2(TableCompressor.IsUsed) = item.hasCompressor2
    '    r2(TableCompressor.GroupID) = item.GroupID
    '    r2(TableCompressor.CompressorName) = " "
    '    dt2.Rows.Add(r2)

    '    r2 = dt2.NewRow
    '    r2(TableCompressor.CompressorID) = 3
    '    r2(TableCompressor.IsUsed) = item.hasCompressor3
    '    r2(TableCompressor.GroupID) = item.GroupID
    '    r2(TableCompressor.CompressorName) = " "
    '    dt2.Rows.Add(r2)

    '    r2 = dt2.NewRow
    '    r2(TableCompressor.CompressorID) = 4
    '    r2(TableCompressor.IsUsed) = item.hasCompressor4
    '    r2(TableCompressor.GroupID) = item.GroupID
    '    r2(TableCompressor.CompressorName) = " "
    '    dt2.Rows.Add(r2)

    '    r2 = dt2.NewRow
    '    r2(TableCompressor.CompressorID) = 5
    '    r2(TableCompressor.IsUsed) = item.hasCompressor5
    '    r2(TableCompressor.GroupID) = item.GroupID
    '    r2(TableCompressor.CompressorName) = " "
    '    dt2.Rows.Add(r2)

    '    r2 = dt2.NewRow
    '    r2(TableCompressor.CompressorID) = 6
    '    r2(TableCompressor.IsUsed) = item.hasCompressor6
    '    r2(TableCompressor.GroupID) = item.GroupID
    '    r2(TableCompressor.CompressorName) = " "
    '    dt2.Rows.Add(r2)

    '    ds.WriteXml(Me._projectConfigFile)

    'End Sub

    Public Sub AddCompressorGroup(ByVal item As ProjectTypeItem)
        Dim strSql As String


        '压缩机组
        strSql = "Insert into tbccCompressorGroup(groupID,groupName,netAddress,ProjectID,listNo,dateCreated,remark) Values(?p0,?p1,?p2,?p3,?p4,?p5,?p6 )"

        Dim p1(6) As System.Data.Common.DbParameter

        item.GroupID = Guid.NewGuid().ToString().Replace("-", "_")

        Dim i As Integer = 0
        p1(i) = New MySql.Data.MySqlClient.MySqlParameter()
        p1(i).ParameterName = "p" + i.ToString : p1(i).Value = item.GroupID
        i += 1

        p1(i) = New MySql.Data.MySqlClient.MySqlParameter()
        p1(i).ParameterName = "p" + i.ToString : p1(i).Value = item.GroupName
        i += 1

        p1(i) = New MySql.Data.MySqlClient.MySqlParameter()
        p1(i).ParameterName = "p" + i.ToString : p1(i).Value = item.NetAddress
        i += 1

        p1(i) = New MySql.Data.MySqlClient.MySqlParameter()
        p1(i).ParameterName = "p" + i.ToString : p1(i).Value = _pi.ProjectID  ' MainForm.pi.ProjectID ' item.projectID
        i += 1

        p1(i) = New MySql.Data.MySqlClient.MySqlParameter()
        p1(i).ParameterName = "p" + i.ToString : p1(i).Value = item.listNo
        i += 1

        p1(i) = New MySql.Data.MySqlClient.MySqlParameter()
        p1(i).ParameterName = "p" + i.ToString : p1(i).Value = item.dateCreated
        i += 1

        p1(i) = New MySql.Data.MySqlClient.MySqlParameter()
        p1(i).ParameterName = "p" + i.ToString : p1(i).Value = item.Remark
        i += 1

        Dim tic As New TBCC.UtilityMobile.TranstrationItem
        tic.Add(strSql, p1)

        '压缩机头
        strSql = "Insert Into tbccCompressor(groupID,CompressorID,CompressorName,isUsed) Values(?p0,?p1,?p2,?p3)"


        For id As Integer = 1 To 6
            i = 0

            Dim p2(3) As MySql.Data.MySqlClient.MySqlParameter

            p2(i) = New MySql.Data.MySqlClient.MySqlParameter()
            p2(i).ParameterName = "p" + i.ToString : p2(i).Value = item.GroupID
            i += 1

            p2(i) = New MySql.Data.MySqlClient.MySqlParameter()
            p2(i).ParameterName = "p" + i.ToString : p2(i).Value = id
            i += 1

            p2(i) = New MySql.Data.MySqlClient.MySqlParameter()
            p2(i).ParameterName = "p" + i.ToString : p2(i).Value = id.ToString() & "#机头"
            i += 1

            p2(i) = New MySql.Data.MySqlClient.MySqlParameter()
            p2(i).ParameterName = "p" + i.ToString : p2(i).Value = IIf(item.hasCompressor(id - 1), 1, 0)
            Debug.Write(p2(i).Value)

            tic.Add(strSql & "--- " & id.ToString() & Now.Ticks.ToString(), p2)
        Next


        '风扇
        strSql = "Insert Into tbccFans(groupID,FanID,FanName,isUsed) Values(?p0,?p1,?p2,?p3)"


        For id As Integer = 1 To 6
            i = 0

            Dim p3(3) As MySql.Data.MySqlClient.MySqlParameter

            p3(i) = New MySql.Data.MySqlClient.MySqlParameter()
            p3(i).ParameterName = "p" + i.ToString : p3(i).Value = item.GroupID
            i += 1

            p3(i) = New MySql.Data.MySqlClient.MySqlParameter()
            p3(i).ParameterName = "p" + i.ToString : p3(i).Value = id
            i += 1

            p3(i) = New MySql.Data.MySqlClient.MySqlParameter()
            p3(i).ParameterName = "p" + i.ToString : p3(i).Value = id.ToString() & "#风扇"
            i += 1

            p3(i) = New MySql.Data.MySqlClient.MySqlParameter()
            p3(i).ParameterName = "p" + i.ToString : p3(i).Value = IIf(item.hasFan(id - 1), 1, 0)
            Debug.Write("Fan" & id & ":" & p3(i).Value)

            tic.Add(strSql & "--- " & id.ToString() & Now.Ticks.ToString(), p3)
        Next

        TBCC.UtilityMobile.SqlHelper.ExecuteTranstration(tic)

    End Sub

    'Public Sub UpdateCompressorGroup(ByVal item As CompressorGroupItem)
    '    Dim ds As DataSet
    '    ds = GetDataset()

    '    Dim filter As String
    '    filter = String.Format("{0}='{1}'", TableCompressorGroup.GroupID, item.GroupID)

    '    For Each r As DataRow In ds.Tables(TableNames.const_table_CompressorGroup).Select(filter)
    '        r(TableCompressorGroup.GroupName) = item.GroupName
    '        r(TableCompressorGroup.NetAddress) = item.NetAddress
    '    Next

    '    For Each r As DataRow In ds.Tables(TableNames.const_table_Compressor).Select(filter)
    '        Select Case r(TableCompressor.CompressorID)
    '            Case 1
    '                r(TableCompressor.IsUsed) = item.hasCompressor1

    '            Case 2
    '                r(TableCompressor.IsUsed) = item.hasCompressor2

    '            Case 3
    '                r(TableCompressor.IsUsed) = item.hasCompressor3

    '            Case 4
    '                r(TableCompressor.IsUsed) = item.hasCompressor4

    '            Case 5
    '                r(TableCompressor.IsUsed) = item.hasCompressor5

    '            Case 6
    '                r(TableCompressor.IsUsed) = item.hasCompressor6

    '        End Select
    '    Next

    '    ds.AcceptChanges()

    '    ds.WriteXml(Me._projectConfigFile)

    'End Sub


    Public Sub UpdateCompressorGroup(ByVal item As ProjectTypeItem)

        Dim strSql As String

        '机组信息
        strSql = "Update tbccCompressorGroup Set groupName=?p0,netAddress=?p1,listNo=?p2,remark=?p3 Where groupid=?p4"

        Dim p1(4) As MySql.Data.MySqlClient.MySqlParameter
        p1(0) = New MySql.Data.MySqlClient.MySqlParameter
        p1(0).ParameterName = "p0" : p1(0).Value = item.GroupName

        p1(1) = New MySql.Data.MySqlClient.MySqlParameter
        p1(1).ParameterName = "p1" : p1(1).Value = item.NetAddress

        p1(2) = New MySql.Data.MySqlClient.MySqlParameter
        p1(2).ParameterName = "p2" : p1(2).Value = item.listNo

        p1(3) = New MySql.Data.MySqlClient.MySqlParameter
        p1(3).ParameterName = "p3" : p1(3).Value = item.Remark

        p1(4) = New MySql.Data.MySqlClient.MySqlParameter
        p1(4).ParameterName = "p4" : p1(4).Value = item.GroupID

        Dim tic As New TBCC.UtilityMobile.TranstrationItem

        tic.Add(strSql, p1)


        '机头信息
        'strSql = "Update tbccCompress0r compressorName=?,isUsed=? Where groupid=? And compressorID=?"
        strSql = "Update tbccCompressor Set isUsed=?p0 Where groupid=?p1 And compressorID=?p2"
        For cID As Integer = 1 To 6
            Dim p2(2) As MySql.Data.MySqlClient.MySqlParameter

            p2(0) = New MySql.Data.MySqlClient.MySqlParameter
            p2(0).ParameterName = "p0" : p2(0).Value = IIf(item.hasCompressor(cID - 1) = True, 1, 0)

            p2(1) = New MySql.Data.MySqlClient.MySqlParameter
            p2(1).ParameterName = "p1" : p2(1).Value = item.GroupID


            p2(2) = New MySql.Data.MySqlClient.MySqlParameter
            p2(2).ParameterName = "p2" : p2(2).Value = cID

            tic.Add(strSql & "--- " & cID, p2)
        Next

        '风扇信息
        strSql = "Update tbccFans Set isUsed=?p0 Where groupid=?p1 And fanID=?p2"
        For fID As Integer = 1 To 6
            Dim p2(2) As MySql.Data.MySqlClient.MySqlParameter

            p2(0) = New MySql.Data.MySqlClient.MySqlParameter
            p2(0).ParameterName = "p0" : p2(0).Value = IIf(item.hasFan(fID - 1) = True, 1, 0)

            p2(1) = New MySql.Data.MySqlClient.MySqlParameter
            p2(1).ParameterName = "p1" : p2(1).Value = item.GroupID


            p2(2) = New MySql.Data.MySqlClient.MySqlParameter
            p2(2).ParameterName = "p2" : p2(2).Value = fID

            tic.Add(strSql & "--- " & fID, p2)
        Next

        TBCC.UtilityMobile.SqlHelper.ExecuteTranstration(tic)

    End Sub

    ''' <summary>
    ''' 删除压缩机组
    ''' </summary>
    ''' <param name="item"></param>
    ''' <remarks></remarks>
    Public Sub DeleteCompressorGroup(ByVal item As ProjectTypeItem)
        Dim strSql As String

        '删除制冷机组表中的数据
        strSql = "Delete From tbccCompressorGroup Where groupid=?p0"
        Dim p(0) As MySql.Data.MySqlClient.MySqlParameter
        p(0) = New MySql.Data.MySqlClient.MySqlParameter
        p(0).ParameterName = "p0" : p(0).Value = item.GroupID

        Dim tic As New TBCC.UtilityMobile.TranstrationItem

        tic.Add(strSql, p)

        '制冷机组的机头配置表
        strSql = "Delete From tbccCompressor Where GroupID=?p0"
        tic.Add(strSql, p)

        '冷凝器风扇配置表
        strSql = "Delete From tbccFans Where GroupID=?p0"
        tic.Add(strSql, p)

        '主工程图中的动态配置的控件
        strSql = "Delete From tbccImageControls Where DataID=?p0"
        tic.Add(strSql, p)

        TBCC.UtilityMobile.SqlHelper.ExecuteTranstration(tic)
    End Sub


    ''' <summary>
    ''' 判断工程中是否包含指定的网络地址
    ''' </summary>
    ''' <param name="netID"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ExistNetaddress(ByVal netID As Byte) As Boolean
        Dim strSql As String
        strSql = "Select count(*) from [tbccCompressorGroup] a where projectID=?p0 and netaddress =?p1"

        Dim p(1) As MySql.Data.MySqlClient.MySqlParameter
        p(0) = New MySql.Data.MySqlClient.MySqlParameter
        p(1) = New MySql.Data.MySqlClient.MySqlParameter

        p(0).ParameterName = "p0" : p(0).Value = Me._pi.ProjectID
        p(1).ParameterName = "p1" : p(1).Value = netID

        Dim obj As Object
        obj = SqlHelper.ExecuteScalar(strSql, p)

        If obj Is Nothing OrElse IsDBNull(obj) Then
            Throw New Exception(String.Format("网络地址{0}已存在", netID))
        End If

        If obj > 0 Then
            Return True
        End If

        strSql = "Select count(*) from tbccCircuit a where projectID=?p0 and netaddress =?p1"
        obj = SqlHelper.ExecuteScalar(strSql, p)

        If obj Is Nothing OrElse IsDBNull(obj) Then
            Throw New Exception(String.Format("网络地址{0}已存在", netID))
        End If

        Return (obj > 0)
    End Function


#Region "Create Table"

    Private Function CreateDatatable_CompressorGroup() As DataTable
        Dim dt As New DataTable("CompressorGroup")
        dt.Columns.Add("GroupID", GetType(String))
        dt.Columns.Add("GroupName", GetType(String))
        dt.Columns.Add("NetAddress", GetType(Byte))
        dt.Columns.Add("DateCreated", GetType(DateTime))
        dt.Columns.Add("ListNo", GetType(Integer))
        Return dt
    End Function

    Private Function CreateDatatable_Compressor() As DataTable
        Dim dt As New DataTable("Compressor")
        dt.Columns.Add("GroupID", GetType(String))
        dt.Columns.Add("CompressorID", GetType(String))
        dt.Columns.Add("CompressorName", GetType(String))
        dt.Columns.Add("IsUsed", GetType(Boolean))
        dt.Columns.Add("ListNo", GetType(Integer))
        Return dt
    End Function

    Private Function CreateDatatable_Condenser() As DataTable
        Dim dt As New DataTable("Condenser")
        dt.Columns.Add("GroupID", GetType(String))
        dt.Columns.Add("CondenserID", GetType(String))
        dt.Columns.Add("CondenserName", GetType(String))
        dt.Columns.Add("DateCrated", GetType(DateTime))
        dt.Columns.Add("ListNo", GetType(Integer))
        Return dt
    End Function

    Private Function CreateDatatable_Fan() As DataTable
        Dim dt As New DataTable("Fan")
        dt.Columns.Add("GroupID", GetType(String))
        dt.Columns.Add("FanID", GetType(String))
        dt.Columns.Add("FanName", GetType(String))
        dt.Columns.Add("DateCrated", GetType(DateTime))
        dt.Columns.Add("ListNo", GetType(Integer))
        Return dt
    End Function

#End Region
End Class
